package com.kfm.loginIp.controller;

import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Aurora雪
 */
@RestController
public class CaptchaController {

    /**
     * 验证码
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GifCaptcha gifCaptcha = new GifCaptcha(130, 48, 4);
        CaptchaUtil.out(gifCaptcha, request, response);
    }

    /**
     * 刷新验证码
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/refreshCaptcha")
    public void refreshCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GifCaptcha gifCaptcha = new GifCaptcha(130, 48, 4);
        CaptchaUtil.out(gifCaptcha, request, response);
    }

    @PostMapping("/captcha")
    public String captcha(String captcha, HttpServletRequest request) {
        // 验证验证码是否正确
        if (CaptchaUtil.ver(captcha, request)) {
            return "success";
        } else {
            return "error";
        }
    }
}
